package com.penghk.hangzhouhouseprice.handler;

import com.penghk.hangzhouhouseprice.entity.HousePrice;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 链家二手房价信息
 * @author penghk
 */
@Component
public class LianjiaSecondHousePriceHandler {
    private Logger logger = LoggerFactory.getLogger(LianjiaSecondHousePriceHandler.class);


    public static final String xihu = "https://hz.lianjia.com/ershoufang/xihu/";
    public static final String xiacheng = "https://hz.lianjia.com/ershoufang/xiacheng/";
    public static final String jianggan = "https://hz.lianjia.com/ershoufang/jianggan/";
    public static final String gongshu = "https://hz.lianjia.com/ershoufang/gongshu/";
    public static final String shangcheng = "https://hz.lianjia.com/ershoufang/shangcheng/";
    public static final String binjiang = "https://hz.lianjia.com/ershoufang/binjiang/";
    public static final String yuhang = "https://hz.lianjia.com/ershoufang/yuhang/";
    public static final String xiaoshan = "https://hz.lianjia.com/ershoufang/xiaoshan/";
    public static final String xiasha = "https://hz.lianjia.com/ershoufang/xiasha/";

    /**
     * 获取所有区域的房价
     * @return
     */
    public List<HousePrice> getAllHousePrice() {
        List<HousePrice> housePriceList = new ArrayList<>();
        housePriceList.addAll(getXiHuPrice());
        housePriceList.addAll(getXiachengPrice());
        housePriceList.addAll(getJiangganPrice());
        housePriceList.addAll(getGongShuPrice());
        housePriceList.addAll(getShangchengPrice());
        housePriceList.addAll(getBinjiangPrice());
        housePriceList.addAll(getYuhangPrice());
        housePriceList.addAll(getXiaoshanPrice());
        housePriceList.addAll(getXiashaPrice());
        return housePriceList;
    }

    /**
     * 获取西湖区房价
     * @return
     * @throws IOException
     */
    public List<HousePrice> getXiHuPrice() {
        return getPrice(xihu , "西湖区");
    }

    /**
     * 获取下城区房价
     * @return
     */
    public List<HousePrice> getXiachengPrice() {
        return getPrice(xiacheng , "下城区");
    }

    /**
     * 获取江干区房价
     * @return
     */
    public List<HousePrice> getJiangganPrice() {
        return getPrice(jianggan , "江干区");
    }


    /**
     * 获取拱墅区房价
     * @return
     */
    public List<HousePrice> getGongShuPrice() {
        return getPrice(gongshu , "拱墅区");
    }

    /**
     * 获取上城区房价
     * @return
     */
    public List<HousePrice> getShangchengPrice() {
        return getPrice(shangcheng , "上城区");
    }


    /**
     * 获取滨江区房价
     * @return
     */
    public List<HousePrice> getBinjiangPrice() {
        return getPrice(binjiang , "滨江区");
    }

    /**
     * 获取萧山区房价
     * @return
     */
    public List<HousePrice> getXiaoshanPrice() {
        return getPrice(xiaoshan , "萧山区");
    }


    /**
     * 获取余杭区房价
     * @return
     */
    public List<HousePrice> getYuhangPrice() {
        return getPrice(yuhang, "余杭区");
    }

    /**
     * 获取下沙区房价
     * @return
     */
    public List<HousePrice> getXiashaPrice() {
        return getPrice(xiasha , "下沙区");
    }

    private List<HousePrice> getPrice(String url , String area) {
        List<HousePrice> housePriceList = new ArrayList<>();

        try {
            Document doc = Jsoup.connect(url).get();
            Elements numEle = doc.select("h2.total span");
            String num = numEle.get(0).text().trim();
            int count = Integer.parseInt(num);
            logger.info("count : " + count);

            Elements head = doc.select("div.unitPrice span");
            int perPageNum = head.size();
            int pages = count / perPageNum;
            int offset = count % perPageNum;
            if (offset > 0) {
                pages++;
            }

            for (int i = 1; i <= pages; i++) {
                String conurl = url + "pg" + i + "/";
                logger.info("page ：" + i + "  url : " + conurl);
                Document nextDoc = Jsoup.connect(conurl).get();
                Elements sinPrices = nextDoc.select("div.unitPrice span");//单价
                Elements sumPrices = nextDoc.select("div.totalPrice span");//总价 : 万
                for (int j = 0; j < sumPrices.size(); j++) {
                    HousePrice housePrice = new HousePrice();
                    housePrice.setArea(area);
                    housePrice.setUuid(UUID.randomUUID().toString());

                    Element sinPrice = sinPrices.get(j);
                    String sinPriceStr = sinPrice.text();
                    int endIndex = sinPriceStr.indexOf("元");
                    if (endIndex < 0) {
                        housePrice.setRemark("信息有误");
                        housePriceList.add(housePrice);
                        continue;
                    }
                    String priceStr = sinPriceStr.substring(2, endIndex);
                    housePrice.setSinPrice(priceStr);

                    Element sumPrice = sumPrices.get(j);
                    String sumPriceStr = sumPrice.text();
                    housePrice.setTotalPrice(sumPriceStr);
                    housePriceList.add(housePrice);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return housePriceList;
        }
        return housePriceList;
    }
}
